<?php


namespace Module\DataAliyunOss\Util;


use ModStart\Core\Input\Response;
use OSS\Core\OssException;
use OSS\OssClient;

class DataAliyunOssUtil
{
    private static function client()
    {
        static $client = null;
        if (null === $client) {
            $client = new OssClient(
                modstart_config('DataAliyunOss_AccessKeyId'),
                modstart_config('DataAliyunOss_AccessKeySecret'),
                modstart_config('DataAliyunOss_Endpoint')
            );
        }
        return $client;
    }

    private static function bucket()
    {
        return modstart_config('DataAliyunOss_Bucket');
    }

    public static function getUrl($url, $timeout = 3600)
    {
        try {
            $url = self::client()->signUrl(self::bucket(), $url, $timeout);
            return Response::generateSuccessData([
                'url' => $url,
            ]);
        } catch (OssException $e) {
            throw $e;
        }
    }
}
